package com.security.door.controller.visitorManage;

import com.github.pagehelper.PageInfo;
import com.security.door.core.wrapper.WrapMapper;
import com.security.door.core.wrapper.Wrapper;
import com.security.door.model.domain.visitorTrip;
import com.security.door.model.dto.visitorTripDto;
import com.security.door.model.vo.visitorTripVo;
import com.security.door.service.IvisitorTripService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.UUID;

@RestController
@Api(value = "WEB - visitorTripController", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class visitorTripController {

    @Autowired
    HttpSession session;
    @Autowired
    HttpServletRequest httpServletRequest;
    @Autowired
    IvisitorTripService ivisitorTripService;

    @GetMapping("/visitor/queryListWithPage")
    @ApiOperation(value = "分页查询所有信息", notes = "分页查询所有信息")
    public Wrapper<PageInfo<visitorTripVo>> listAll(visitorTripDto entityDto) {
        int pageIndex = httpServletRequest.getParameter("start") == null ? 0 : Integer.parseInt(httpServletRequest.getParameter("start"));
        int pageSize = httpServletRequest.getParameter("length") == null ? Integer.MAX_VALUE : Integer.parseInt(httpServletRequest.getParameter("length"));
        String searchStr = httpServletRequest.getParameter("search[value]");
        entityDto.setKeyWord(searchStr);
        entityDto.setPageIndex(pageIndex / pageSize + 1);
        entityDto.setPageSize(pageSize);
        var list = ivisitorTripService.queryListWithPage(entityDto);
        return WrapMapper.ok(list);
    }

    @PostMapping("/visitor/saveOrUpdate")
    @ApiOperation(value = "保存和更新", notes = "保存和更新")
    public Wrapper save(@RequestBody visitorTrip entity) {
        if (null != entity.getGuid() && !entity.getGuid().isBlank()) {
            ivisitorTripService.update(entity);
        } else {
            entity.setGuid(UUID.randomUUID().toString());
            entity.setCreatetime(new Date());
            ivisitorTripService.save(entity);
        }
        return WrapMapper.ok(entity);
    }

    @PostMapping("/visitor/selectByStart")
    @ApiOperation(value = "获取当月所有事件", notes = "获取当月所有事件")
    public Wrapper selectByMonth(@RequestBody visitorTrip entity) {
        var list = ivisitorTripService.selectByStart(entity);
        return WrapMapper.ok(list);
    }

    @GetMapping("/visitor/delEvent/{guid}")
    @ApiOperation(value = "获取当月所有事件", notes = "获取当月所有事件")
    public Wrapper delEvent(@PathVariable("guid") String guid) {
        return WrapMapper.ok(ivisitorTripService.deleteByKey(guid));
    }

    @GetMapping("/visitor/getEvent/{guid}")
    @ApiOperation(value = "获取单个事件", notes = "获取单个事件")
    public Wrapper getEvent(@PathVariable("guid") String guid) {
        return WrapMapper.ok(ivisitorTripService.selectByKey(guid));
    }
}
